package com.yiran.web.controller.api;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

import com.alibaba.fastjson.JSON;
import com.yiran.common.base.ResultWrapper;
import com.yiran.common.config.Global;
import com.yiran.framework.shiro.service.SysPasswordService;
import com.yiran.framework.util.JwtUtils;
import com.yiran.framework.web.base.BaseController;
import com.yiran.system.domain.SysUser;
import com.yiran.system.service.ISysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.WebAsyncTask;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

@RestController
@RequestMapping("/open/test")
@Api(value="Open测试接口", description="开放API测试接口")
public class OpenTestController extends BaseController {
	@Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysPasswordService sysPasswordService;
    @Autowired
    @Qualifier("taskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    @GetMapping(value = "/test")
    @ApiOperation(value="测试Callable", notes="测试 Callable; Callable类型 swagger 不能使用try,故实际开发建议使用 WebAsyncTask ,后者也是要强大的多")
    public Callable test(){
        System.out.println(new Date());
        return ()->{
            return success("123456");
        };
    }
   
   /* @PostMapping("loginUser")
    @ApiOperation("登录")
    public ResultWrapper<Map<String,Object>> login(@RequestBody LoginForm form) throws Exception{
    	Map<String, Object> map = new HashMap<>();
    	System.out.println("api登录请求参数："+JSON.toJSONString(form));
    	//生成token
        String app_token =null;
        //用户登录
    	SysUser user = sysUserService.selectUserByPhoneNumber(form.getMobile());
    	if(user!=null){
    		String password = sysPasswordService.encryptPassword(user.getLoginName(), form.getPassword(), user.getSalt());
    		System.out.println("加密后的密码："+password);
    		if(!password.equals(user.getPassword())){
    			return ResultWrapper.error().newInstance("0003", "登录密码错误");
    		}
    		app_token = JwtUtils.createToken(user.getUserName());
    		map.put("token", app_token);
            map.put("expire", Global.getJwtOutTime());
            map.put("user", user);
    	}else{
    		return ResultWrapper.error().newInstance("0003", "用户名不存在");
    	}
        return ResultWrapper.ok().putData(map);
    }*/
   /* @PostMapping(value = "/test2")
    @ApiOperation(value="测试WebAsyncTask", notes="测试WebAsyncTask")
    public WebAsyncTask test2(HttpServletRequest request){
        // 打印处理线程名
        System.out.println(String.format("请求处理线程：%s", Thread.currentThread().getName()));

        // 模拟开启一个异步任务，超时时间为10s
        String TIME_MESSAGE = "ok";
        WebAsyncTask<String> asyncTask = new WebAsyncTask<>(10 * 1000L,taskExecutor, () -> {
            System.out.println(String.format("异步工作线程：%s", Thread.currentThread().getName()));
            // 任务处理时间5s，不超时
            ArrayList<SysTest> objects = new ArrayList<>();
            Snowflake snowflake = IdUtil.createSnowflake(1, 1);
            for (int i = 0; i <5 ; i++) {
                long l = snowflake.nextId();
                SysTest sysTest = new SysTest("" + i, "real" + i);
                sysTest.setId(l+"");
                objects.add(sysTest);
            }
            sysTestServiceI.saveBatch(objects);
            objects.forEach(System.out::println);
            return TIME_MESSAGE;
        });

        // 任务执行完成时调用该方法
        asyncTask.onCompletion(() -> System.out.println("任务执行完成"));
        asyncTask.onTimeout(() -> {
            System.out.println("任务执行超时");
            return TIME_MESSAGE;
        });

        System.out.println("继续处理其他事情");
        return asyncTask;
    }*/
    @PostMapping(value = "/test3")
    @ApiOperation(value="测试body", notes="测试body")
    public WebAsyncTask test3(HttpServletRequest request,@RequestBody String bodyData){
        WebAsyncTask test2 = new WebAsyncTask( () -> {
            String test = request.getParameter("test");
            System.out.println("dddddddddd");
            return bodyData;
        });
        return test2;
    }
    @PostMapping(value = "/test4")
    @ApiOperation(value="测试beanBody", notes="测试beanBody")
    public WebAsyncTask test4(HttpServletRequest request,@RequestBody SysUser SysUser){
        WebAsyncTask test2 = new WebAsyncTask( () -> {
            return SysUser;
        });
        return test2;
    }
    @PostMapping(value = "/test5")
    @ApiOperation(value="测试bean", notes="测试bean")
    public WebAsyncTask test5(HttpServletRequest request, SysUser SysUser){
        WebAsyncTask test2 = new WebAsyncTask( () -> {
            return SysUser;
        });
        return test2;
    }

}